package com.cqnu.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.transaction.TransactionException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.support.HandlerMethodInvocationException;

import com.cqnu.entity.commonModel.PageParameters;
import com.cqnu.service.IMajorService;
import com.cqnu.service.IMenuService;
import com.cqnu.service.IPaperLogService;
import com.cqnu.service.IPaperService;
import com.cqnu.service.IStudentService;
import com.cqnu.service.ISuperUserService;
import com.cqnu.service.ITeacherService;
import com.cqnu.service.IThesisService;
import com.cqnu.service.IThesisStudentService;




/**
 * 
 */
public class BaseController {
	protected static final Logger logger = LoggerFactory.getLogger(BaseController.class);

	@Autowired
	protected IMenuService				iMenuService;
	@Autowired
	protected IStudentService 			studentService;
	@Autowired
	protected ITeacherService 			teacherService;
	@Autowired
	protected ISuperUserService 		superUserService;
	@Autowired
	protected IPaperService 			paperService;
	@Autowired
	protected IMajorService 			majorService;
	@Autowired
	protected IThesisService 			thesisService;
	@Autowired
	protected IThesisStudentService 	thesisStudentService;
	@Autowired
	protected IPaperLogService 			paperLogService;


	/**
	 * 将对象放到Session中
	 * 
	 * @param request
	 * @param key
	 * @param obj
	 */
	public void setSession(HttpServletRequest request, String key, Object obj) {
		request.getSession().setAttribute(key, obj);
	}

	/**
	 * 从Session中取对象
	 * 
	 * @param request
	 * @param key
	 */
	public Object getSession(HttpServletRequest request, String key) {
		if (request != null) {
			return request.getSession().getAttribute(key);
		}
		return null;
	}

	/**
	 * 移除Session中的对象
	 * 
	 * @param request
	 * @param key
	 */
	public void removeSession(HttpServletRequest request, String key) {
		if (request != null) {
			request.getSession().removeAttribute(key);
		}
	}

	/**
	 * 取用户IP
	 * 
	 * @param request
	 */
	public String clientIP(HttpServletRequest request) {
		return request.getRemoteAddr();
	}

	@ExceptionHandler
	public String exp(HttpServletRequest request, Exception ex) {
		request.setAttribute("errorMsg", ex);
		logger.error(ex.getMessage());
		if (ex instanceof DataAccessException || ex instanceof TransactionException) {
			return "error/dbErr";
		} else if (ex instanceof HandlerMethodInvocationException) {
			request.setAttribute("errorMsg", "访问方法时异常");
		}
		return "error/500";
	}
	
	protected Map<String,Object> getMap(PageParameters par){
		Map<String,Object> map = new HashMap<String, Object>();
		if(null != par){
			map.put("start", (par.getPage()-1)*par.getRows());
			map.put("size", par.getRows());
		}
		return map;
	}
	
	protected Map<String,Object> clearPar(Map<String,Object> map){
		if(null != map){
			map.remove("start");
			map.remove("size");
		}
		return map;
	}

}
